//
//  JKTextView.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/12.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKTextView: UITextView {
    
    /** 占位label */
    private(set) lazy var placeHolderLabel: UILabel = {
        
        let placeHolderLabel = UILabel()
        
        addSubview(placeHolderLabel)
        placeHolderLabel.font = font
        placeHolderLabel.textColor = JKSameRGBColor(217)
        
        return placeHolderLabel
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        placeHolderLabel.sizeToFit()
        
        placeHolderLabel.x = 5
        placeHolderLabel.y = 8
    }
    
    // MARK: - 初始化
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        initialization()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialization()
    }
    
    private func initialization() {
        
        delegate = self
    }
    
    // MARK: - 更新皮肤
    public func changeToNightSkin() { // 夜间
        
        backgroundColor = JKNightSkinBgColor
        textColor = JKColor(186, 186, 186)
        placeHolderLabel.textColor = JKColor(68, 68, 68)
        keyboardAppearance = UIKeyboardAppearance.dark
        resignFirstResponder()
        becomeFirstResponder()
    }
    
    public func changeToDaySkin() { // 白天
        
        placeHolderLabel.textColor = JKSameRGBColor(217)
        textColor = UIColor.black
        backgroundColor = JKGlobalBgColor
        keyboardAppearance = UIKeyboardAppearance.default
        resignFirstResponder()
        becomeFirstResponder()
    }
}

extension JKTextView: UITextViewDelegate{
    
    func textViewDidChange(_ textView: UITextView) {
        
        placeHolderLabel.isHidden = hasText
    }
}
